package com.wang.myblog.controller;

import com.alibaba.fastjson.JSON;
import com.wang.myblog.common.ResponseResult;
import com.wang.myblog.entity.Admin;
import com.wang.myblog.service.AdminService;
import com.wang.myblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private CommentService commentService;

    @RequestMapping("/login")
    public String login(){
        return "admin/login";
    }

    @PostMapping("/dologin")
    @ResponseBody
    public String dologin(@RequestParam("name")String name, @RequestParam("pwd") String pwd, HttpSession session){
        System.out.println("登录");
        ResponseResult result=new ResponseResult();
        if(adminService.login(name,pwd)){
            session.setAttribute("admin",adminService.queryById(1));
            session.setAttribute("unReadCount",commentService.getUnReadCount());
            session.setAttribute("unReadComment",commentService.queryUnRead(0,4));
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("名称或密码错误");
        }
        System.out.println(JSON.toJSONString(result));
        return JSON.toJSONString(result);
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("admin");
        return "admin/login";
    }


    @RequestMapping("/adminInfo")
    public String adminInfo(Model model){
        Admin admin=adminService.queryById(1);
        model.addAttribute("admin",admin);
        return "admin/adminInfo";
    }

    @RequestMapping("/setPwd")
    @ResponseBody
    public String setPwd( @RequestParam("pwd1") String pwd1,@RequestParam("sPassword") String sPassword){
        System.out.println(pwd1);
        Admin admin=adminService.queryById(1);
        ResponseResult result=new ResponseResult();
        if(admin.getPassword().equals(pwd1)){
            admin.setPassword(sPassword);
            adminService.update(admin);
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("旧密码错误");
        }
        return JSON.toJSONString(result);
    }

    @RequestMapping("/setInfo")
    @ResponseBody
    public String setInfo(@RequestBody Admin admin, HttpSession session){
        ResponseResult result=new ResponseResult();
        Admin admin1=adminService.queryById(1);
        System.out.println(admin);
        admin1.setImg(admin.getImg());
        admin1.setName(admin.getName());
        admin1.setAddress(admin.getAddress());
        admin1.setCareer(admin.getCareer());
        admin1.setSex(admin.getSex());
        admin1.setInfo(admin.getInfo());
        admin1.setBirthday(admin.getBirthday());
        System.out.println(admin1);

        //System.out.println(admin.getBirthday());
        //System.out.println(admin1.getBirthday());
        if(adminService.update(admin1)){
            session.setAttribute("admin",admin1);
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("更新失败");
        }
        return JSON.toJSONString(result);
    }

    @RequestMapping("/setContact")
    @ResponseBody
    public String setContact(String wechat,String email,String phone,String qq){
        Admin admin=adminService.queryById(1);
        admin.setWechat(wechat);
        admin.setEmail(email);
        admin.setPhone(phone);
        admin.setQq(qq);

        ResponseResult result=new ResponseResult();
        if(adminService.update(admin)){
            result.setCode(200);
        }else {
            result.setCode(500);
            result.setMessage("更新失败");
        }
        return JSON.toJSONString(result);
    }
}
